﻿/* ==============================================================================
 * 功能描述：ResManager  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/3 23:53:23
 * ==============================================================================*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace ZZQFrameWork
{
    public class ResManager : Singleton<ResManager>
    {
        /// <summary>
        /// 资源信息
        /// </summary>
        public class AssetInfo
        {
            /// <summary>
            /// 资源名称
            /// </summary>
            public string assetName;

            /// <summary>
            /// 资源路径
            /// </summary>
            public string assetPath;

            /// <summary>
            /// 资源
            /// </summary>
            public object asset;

            public AssetInfo(string name)
            {
                assetName = name;
                assetPath = ResManager.Instance.GetFileFullName(name);
                //Debug.Log(assetPath);
            }

            /// <summary>
            /// 加载资源
            /// </summary>
            public void Load()
            {
                asset = Resources.Load(assetPath);
            }
        }

        #region 根据资源名称获取资源路径

        /// <summary>
        /// 资源名称和路径集合
        /// </summary>
        private Dictionary<string, string> mAssetPathDic = new Dictionary<string, string>();

        /// <summary>
        /// 获取资源的完整路径
        /// </summary>
        /// <param name="assetName"> 资源名称 </param>
        /// <returns></returns>
        public string GetFileFullName(string assetName)
        {
            string parent = "";
            if (!GetParentPathName(assetName, ref parent))
            {
                Debug.LogError("你要a加载的文件不存在Resources当中D assetName =" + assetName);
            }
            return parent == "" ? assetName : parent + "/" + assetName;
        }

        /// <summary>
        /// 获取资源的父目录
        /// </summary>
        /// <param name="assetName"> 资源名</param>
        /// <param name="r"> 返回的字符串 </param>
        /// <returns></returns>
        public bool GetParentPathName(string assetName, ref string r)
        {
            if (mAssetPathDic.Count == 0)
            {
                TextAsset tex = Resources.Load<TextAsset>("res");
                StringReader sr = new StringReader(tex.text);
                string fileName = sr.ReadLine();
                while (fileName != null)
                {
                    //Debug.Log("fileName =" + fileName);
                    string[] ss = fileName.Split('=');
                    mAssetPathDic.Add(ss[0], ss[1]);
                    fileName = sr.ReadLine();
                }
            }

            if (mAssetPathDic.ContainsKey(assetName))
            {
                r = mAssetPathDic[assetName];
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

        /// <summary>
        /// 已经加载的资源集合
        /// </summary>
        public Dictionary<string, AssetInfo> resDic = null;

        ///// <summary>
        ///// 等待加载资源集合
        ///// </summary>
        //public Stack<AssetInfo> waitResStack = null; 

        protected override void Init()
        {
            resDic = new Dictionary<string, AssetInfo>();
            //waitResStack=new Stack<AssetInfo>();
        }

        /// <summary>
        /// 根据资源名称从resDic中获取资源
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private AssetInfo GetAssetInfoByName(string name)
        {
            if (resDic.ContainsKey(name))
            {
                return resDic[name];
            }
            return null;
        }

        /// <summary>
        /// 加载资源
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public object Load(string name)
        {
            AssetInfo info = GetAssetInfoByName(name);
            if (info == null)
            {
                info = new AssetInfo(name);
                info.Load();
                if (info.asset == null)
                {
                    Debug.LogError("not load object,path:" + info.assetPath);
                    return null;
                }
                else
                {
                    resDic.Add(name, info);
                    return info.asset;
                }
            }
            return info.asset;
        }

    }
}